// JavaScript Document
function lastPostFunc(page, wordsearch, newstype, productstatus, categoryid, tradeid, provinceid, modifytype, clubid) 
{ 
	$('#loading').show();
	$('#loadding-text').show();
	
	var last = $('ul').find("li:last");
	var last_id = last.attr('last_id');
	

	$.post('validate/'+page+'Data.php',
		{ 'id': last_id ,
		  'wordSearch': wordsearch, 
		  'news_type': newstype,
		  'product_status': productstatus,
		  'category_id': categoryid,
		  'trade_id': tradeid,
		  'province_id' : provinceid,	
		  'modify_id': modifytype,
		  'clubid': clubid	
		},
		function(data)
		{
			//alert(data);		
			if (data != "") 
			{	
				//alert('Not Null');	
				//alert(last_id);						
				last.after(data);
				$('#loading').hide(); 
				$('#loadding-text').hide();		
			}
			return false;
		}
	);
};

function lastReply(page, postId, numReply) 
{ 	
	var last_id = $('.countReply:last').data('replyid')
	
	//alert(last_id);

	$.post('validate/'+page+'Data.php',
		{ 
			'reply_id': last_id ,
			'post_id': postId,
			'num_reply': numReply
		
		
		},
		function(data)
		{		
			if (data != "") 
			{		
				//alert(last_id);						
				$('.countReply:last').after(data);
			}
			else
			{
				$('#loading').hide();
				$('#loadding-text').hide();					
			}
			return false;
		}
	);
};

var killScroll = false; // IMPORTANT
$(window).scroll(function(){ // IMPORTANT
		if  ($(window).scrollTop() >= ($(document).height() - ($(window).height()))){ // IMPORTANT
				if (killScroll == false) { // IMPORTANT - Keeps the loader from fetching more than once.
						killScroll = true; // IMPORTANT - Set killScroll to true, to make sure we do not trigger this code again before it's done running.
						
						
			$('#loading').show();
			$('#loadding-text').show();			
			//alert('Last Page - '+$("li:last").attr("id"));	
			$('#loading').html('<img src="images/loading.gif" alt="" width="150" height="13" />');
						
			if( $("li:last").data('ctype') == 'market')
			{
				setTimeout(function(){lastPostFunc('market', '', '', '', '', '', '', '' ); $('#loading').hide(); $('#loadding-text').hide(); },800);
			}
			
			if($("li:last").data('ctype') == 'marketSearch')
			{
				alert('Mmarket Search');				
				//alert($("li:last").attr('last_id'));
				setTimeout
				(
					function()
						{
							lastPostFunc
							(
								'marketSearch', 
								$("li:last").data('wordsearch'), '', 
								$("li:last").data('productstatus'), 
								$("li:last").data('categoryid'),
								$("li:last").data('tradeid'),
								$("li:last").data('provinceid'),''
							); 
							
							$('#loading').hide(); 
							$('#loadding-text').hide(); 
						},100
				);
			}
			
			if($("li:last").data('ctype') == 'news')
			{
				//lastPostFunc('news','');
				setTimeout(function(){ lastPostFunc('news','', '', '', '', '', '', ''); $('#loading').hide(); $('#loadding-text').hide(); },800);
			}
			
			if($("li:last").data('ctype') == 'newsSearch')
			{
				//lastPostFunc('newsSearch', $("li:last").data('wordsearch'));
				setTimeout(function(){ lastPostFunc('newsSearch', $("li:last").data('wordsearch'), $('li:last').data('newstype'), '', '', '', '', ''); $('#loading').hide(); $('#loadding-text').hide(); },800);
			}
			
			if($("li:last").data('ctype') == 'gallery')
			{
				//lastPostFunc('gallery','');
				setTimeout(function(){ lastPostFunc('gallery','', '', '', '', '', '', ''); $('#loading').hide(); $('#loadding-text').hide(); },800);
			}	
			
			if($("li:last").data('ctype') == 'gallerySearch')
			{
				//lastPostFunc('gallerySearch', $("li:last").data('wordsearch'));
				setTimeout(function(){ lastPostFunc('gallerySearch', $("li:last").data('wordsearch'), '', '', '', '', '', ''); $('#loading').hide(); $('#loadding-text').hide(); },800);
			}
			
			if($("li:last").data('ctype') == 'club')
			{
				//lastPostFunc('club','');
				setTimeout(function(){ lastPostFunc('club','', '', '', '', '', '', ''); $('#loading').hide(); $('#loadding-text').hide(); },800);
			}
			
			if($("li:last").data('ctype') == 'clubSearch')
			{
				//lastPostFunc('clubSearch', $("li:last").data('wordsearch'));
				setTimeout(function(){ lastPostFunc('clubSearch', $("li:last").data('wordsearch'), '', '', '', '', '', ''); $('#loading').hide(); $('#loadding-text').hide(); },800);
			}
			
			if($("li:last").data('ctype') == 'modify')
			{
				//lastPostFunc('club','');
				setTimeout(function(){ lastPostFunc('modify','', '', '', '', '', '', ''); $('#loading').hide(); $('#loadding-text').hide(); },800);
			}	
			
			if($("li:last").data('ctype') == 'modifySearch')
			{
				//lastPostFunc('clubSearch', $("li:last").data('wordsearch'));
				setTimeout
				(
					function()
						{ 
							lastPostFunc
							(
								'modifySearch', 
								$("li:last").data('wordsearch'), '', '', '', '', '',
								$("li:last").data('modifytype')
							);
								 
							$('#loading').hide(); 
							$('#loadding-text').hide(); 
						},800
				);
			}
			
			if($("li:last").data('ctype') == 'activity')
			{
				setTimeout(function(){ lastPostFunc('activity','', '', '', '', '', '', ''); $('#loading').hide(); $('#loadding-text').hide(); },800);
			}
			
			if($("li:last").data('ctype') == 'clubListDetail')
			{
				setTimeout(function(){ lastPostFunc('clubListDetail','', '', '', '', '', '', '', $("li:last").data('clubid')); $('#loading').hide(); $('#loadding-text').hide(); },800);
			}
			
			/*################# */	

			if($('.countReply:last').data('ctype') == 'reply_club')
			{
				setTimeout(function(){ lastReply('replyClub', $('.countReply:last').data('postid'), $('.countReply:last').data('numreply'));  $('#loading').hide(); $('#loadding-text').hide(); },800);
			}
			
			if($('.countReply:last').data('ctype') == 'reply_news')
			{
				setTimeout
				(
					function()
					{ 
						lastReply
						(
							'replyNews', $('.countReply:last').data('newsid'), 
							$('.countReply:last').data('numreply')
						);  
						
						$('#loading').hide(); 
						$('#loadding-text').hide();
						 
					},800
				);
			}
			
			if($('.countReply:last').data('ctype') == 'reply_product')
			{
				setTimeout
				(
					function()
					{ 
						lastReply
						(
							'replyProduct', $('.countReply:last').data('productid'), 
							$('.countReply:last').data('numreply')
						);  
						
						$('#loading').hide(); 
						$('#loadding-text').hide();
						 
					},800
				);
			}
			
			if($('.countReply:last').data('ctype') == 'reply_modify')
			{
				setTimeout
				(
					function()
					{ 
						lastReply
						(
							'replyModify', $('.countReply:last').data('modifyid'), 
							$('.countReply:last').data('numreply')
						);  
						
						$('#loading').hide(); 
						$('#loadding-text').hide();
						 
					},800
				);
			}	
			
			if($('.countReply:last').data('ctype') == 'reply_gallery')
			{
				setTimeout
				(
					function()
					{ 
						lastReply
						(
							'replyGallery', $('.countReply:last').data('galleryid'), 
							$('.countReply:last').data('numreply')
						);  
						
						$('#loading').hide(); 
						$('#loadding-text').hide();
						 
					},800
				);
			}						
			
			//console.log('aa');
			setTimeout(function(){killScroll = false},500);
		}
	}
}); 